/**
 * Created by hcadmin on 16/4/25.
 */

define([
        "jquery",
        "IO",
        "bootbox"
    ], function ($, IO, bootbox) {
        gettable();


        $("#newact").on("click", function () {
            $('#myModal1').modal('show');


            $("#textName1").val('');
            $("#datetimepicker1").val('');
            $("#groupNum1").val('');
            $("#groupLeader1").val('');
        });

        $("#newbtn").on('click',function(){
            var activity_name=$("#textName1").val();
            var activity_time=$("#datetimepicker1").val();
            var activity_group=$("#groupNum1").val();
            var activity_coach=$("#groupLeader1").val();

            var params={};
            params.activity_name=activity_name;
            params.activity_time=activity_time;
            params.activity_group=activity_group;
            params.activity_coach=activity_coach;

            IO.post("/shapan/api/activity",params,function(d){

                if (d.code == "0")
                {
                    $('#myModal1').modal('hide');
                    gettable();

                }
                else {
                    $('#myModal1').modal('hide');
                    bootbox.alert(d.msg);
                }
            })
        })
        //function addTr(tab,row,trHtml){
        //    //获取table最后一行 $("#tab tr:last")
        //    //获取table第一行 $("#tab tr").eq(0)
        //    //获取table倒数第二行 $("#tab tr").eq(-2)
        //    var $tr=$("#"+tab+" tr").eq(row);
        //    if($tr.size()==0){
        //        alert("指定的table id或行数不存在！");
        //        return;
        //    }
        //    $tr.after(trHtml);
        //}


        $(document).on('click', '.editaa', function () {
            //var rows = data.data.rows;
            //var rows=$(this).attr('')
            //var index = $(this).attr('row_id');
            var activity_name=$(this).attr('act_name');
            var activity_mid=$(this).attr('act_id');
            var activity_time=$(this).attr('act_time');
            var activity_group=$(this).attr('act_group');
            var activity_coach=$(this).attr('act_coach');




            $('#textName').val(activity_name);
            $('#datetimepicker').val(activity_time);
            $('#groupNum').val(activity_group);
            $('#groupLeader').val(activity_coach);

            $("#myModal").modal('show');
            $("#btnUpdate").unbind('click');
            update(activity_mid);

        });



        function gettable() {
            var grid;
            var columns = [
                {
                    title: '活动编号',
                    dataIndex: 'activity_mid',
                    dataSource: function (data, datarow) {
                        //return "<span>"+data[datarow].activity_name+"</span>

                        return "<span>" + data[datarow].id + "</span>";
                    }
                },
                {
                    title: '活动名称',
                    dataIndex: 'activity_name',
                    dataSource: function (data, datarow) {
                        console.log(data[datarow].activity_name);
                        return "<span>" + data[datarow].activity_name + "</span>";
                    }
                },


                {
                    title: '进入活动',
                    dataIndex: 'into',
                    dataSource: function (data, datarow) {

                        return "<a href='activity/importUser?activity_mid="
                            + data[datarow].mid + "'  style='color: #5186b2; font-size: 16px;'>进入活动</a>";
                    }
                },
                {
                    title: '操作',
                    dataIndex: 'action',
                    dataSource: function (data, datarow) {
                        return "<img src='/images/edit.png'/>&nbsp;" +
                            "<a href='' class='editaa' row_id='" + datarow + "'  act_name='" + data[datarow].activity_name + "'    act_id='" + data[datarow].mid + "'     act_time='" +data[datarow].activity_time +"'     act_group='"+data[datarow].activity_group+"'   act_coach='"+data[datarow].activity_coach+"'          data-toggle='modal'  style='color: #5186b2; font-size: 16px;'>编辑</a>"
                            + '&nbsp;&nbsp;&nbsp;&nbsp;' +
                            "<a href='javascript:(0)' act_id='"+data[datarow].mid+"'  class='deleterow' style='font-size: 16px;color: #e32332'>删除</a>";
                    }
                }
            ];

            function dataInit() {
                grid = new Grid("/shapan/api/huodongFenpei", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);
                grid.Init();
            }
            //获取输入框内容
            function get_input_param() {

                var activity_find = $("#activity_find").val();
                var param = "activity_find=" + activity_find;
                return param;

            }
            dataInit();
            //查询
            $("#searchid1").bind("click", function () {
                gettable();
            });



        }

        function update(activity_mid) {

            $("#btnUpdate").click(function () {
                $('#myModal').modal('hide');
                var params = {};
                var activity_name = $("#textName").val();
                var activity_time = $("#datetimepicker").val();
                var group_num = $("#groupNum").val();
                var group_leader = $("#groupLeader").val();

                params.activity_name = activity_name;
                params.activity_time = activity_time;
                params.activity_coach = group_leader;
                params.activity_group = group_num;
                params.activity_mid=activity_mid;
                console.log('hjlll');
                console.log(params);

                if (!activity_name || !activity_time || !group_num || !group_leader) {
                    bootbox.alert("请填写完整信息!");
                }
                //console.log(params);

                    IO.post("/shapan/api/activityedit", params, function (d) {
                        if (d.code == "0") {//$('.page_control .selected').attr('dataindex');

                           gettable();

                        }
                        else {
                            bootbox.alert(d.msg);

                        }

                    });

                //else {
                //    IO.post("/shapan/api/activity", params, function (d) {
                //        if (d.code == "0") {
                //            $('#myModal').modal('hide');
                //            //$('.page_control .selected').attr('dataindex');
                //
                //            //gettable($('.page_control .selected').attr('dataindex'), activity_mid);
                //        }
                //        else {
                //            $('#myModal').modal('hide');
                //            bootbox.alert(d.msg);
                //        }
                //
                //    });
                //}


            })

        }
        $(document).on('click', '.deleterow', function (e) {
            var mid=$(this).attr('act_id');

            bootbox.confirm('确定删除该活动？', function (result) {
                if (result) {

                    var activity_mid = mid;
                    var params = {activity_mid: activity_mid}
                    //console.log(params)

                    IO.post('/shapan/api/activityedit2', params, function (d) {
                        if (d.code == "0") {

                            //$('.page_control .selected').attr('dataindex');
                            gettable();
                        }
                        else {

                            bootbox.alert(d.msg);
                        }
                    })
                }

            })
        })
    }

);